<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>头像上传</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input[type="file"], input[type="number"] {
            width: 100%;
            padding: 8px;
            box-sizing: border-box;
        }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background-color: #45a049;
        }
        .preview {
            margin-top: 20px;
            border: 1px solid #ddd;
            padding: 10px;
            display: none;
        }
        .preview img {
            max-width: 100%;
            max-height: 300px;
        }
        .result {
            margin-top: 20px;
            padding: 10px;
            border: 1px solid #ddd;
            display: none;
        }
        .success {
            background-color: #dff0d8;
            color: #3c763d;
        }
        .error {
            background-color: #f2dede;
            color: #a94442;
        }
    </style>
</head>
<body>
    <h1>员工头像上传</h1>
    
    <form id="uploadForm" enctype="multipart/form-data">
        <div class="form-group">
            <label for="employeeId">员工ID：</label>
            <input type="number" id="employeeId" name="employeeId" required min="1">
        </div>
        
        <div class="form-group">
            <label for="avatarFile">选择头像图片：</label>
            <input type="file" id="avatarFile" name="file" accept="image/*" required>
        </div>
        
        <button type="submit">上传头像</button>
    </form>
    
    <div class="preview" id="imagePreview">
        <h3>图片预览</h3>
        <img id="previewImg" src="" alt="预览图片">
    </div>
    
    <div class="result" id="resultBox"></div>
    
    <script>
        // 图片预览功能
        document.getElementById('avatarFile').addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (file) {
                const reader = new FileReader();
                reader.onload = function(e) {
                    document.getElementById('previewImg').src = e.target.result;
                    document.getElementById('imagePreview').style.display = 'block';
                }
                reader.readAsDataURL(file);
            }
        });
        
        // 表单提交
        document.getElementById('uploadForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const employeeId = document.getElementById('employeeId').value;
            const fileInput = document.getElementById('avatarFile');
            const file = fileInput.files[0];
            
            if (!employeeId || !file) {
                showResult('请填写所有必填字段', false);
                return;
            }
            
            const formData = new FormData();
            formData.append('file', file);
            formData.append('employeeId', employeeId);
            
            // 发送请求
            fetch('/files/avatar', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
        if (data.result === 'SUCCESS') {
            showResult('头像上传成功：' + data.data, true);
            // 显示上传成功的头像
            const avatarPath = data.data;
            if (avatarPath) {
                // 从返回的路径中提取文件名
                const filename = avatarPath.substring(avatarPath.lastIndexOf('/') + 1);
                // 使用与后端 FileController 一致的路径格式
                const avatarUrl = '/files/avatar/view/' + filename;
                
                // 创建头像显示区域
                let avatarDisplay = document.getElementById('avatarDisplay');
                if (!avatarDisplay) {
                    avatarDisplay = document.createElement('div');
                    avatarDisplay.id = 'avatarDisplay';
                    avatarDisplay.innerHTML = '<h3>上传成功的头像</h3><img id="uploadedAvatar" style="max-width: 200px; max-height: 200px; border: 1px solid #ddd;" />';
                    document.body.appendChild(avatarDisplay);
                }
                
                // 显示头像
                document.getElementById('uploadedAvatar').src = avatarUrl;
                avatarDisplay.style.display = 'block';
            }
        } else {
            showResult('上传失败：' + data.message, false);
        }
    }
            .catch(error => {
                showResult('上传出错：' + error.message, false);
            });
        });
        
        // 显示结果
        function showResult(message, isSuccess) {
            const resultBox = document.getElementById('resultBox');
            resultBox.textContent = message;
            resultBox.style.display = 'block';
            
            if (isSuccess) {
                resultBox.className = 'result success';
            } else {
                resultBox.className = 'result error';
            }
        }
    </script>
</body>
</html>